home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / linux-bo / chkconfi.000 / chkconfi / chkconfig-0.3b / chkconfig.c < prev    next >
C/C++ Source or Header  |  1996-01-08  |  5KB  |  200 lines

  1. /* chkconfig - checks a pre-set directory (default is /etc/config) for
  2.  * flag-files containing "on" or "off" (OR, more precisely, "on" or
  3.  * ANYTHING ELSE.
  4.  *
  5.  * Copyright (c) 1995 Marc A. Volovic (marc@leonardo.ls.huji.ac.il)
  6.  *
  7.  * Released under GPL.
  8.  * 
  9.  * If the flag file contains "on", TRUE is returned.
  10.  *
  11.  * If the invocation contains a third argument, changes the flag file
  12.  * to the give
  13.  *
  14.  * This is a wholesale filch from the IRIX chkconfig, allowing various
  15.  * daemons to be activated/deactivated at will, without mucking with
  16.  * /etc/rc.d/rc.* overmuch.
  17.  *
  18.  * History
  19.  *
  20.  * 0.01a - works as advertised :-).
  21.  *
  22.  * 0.01b - added an additional error check.
  23.  *
  24.  * 0.01c - added hooks for modification of config flags.
  25.  *
  26.  * 0.01d - flag modification works.
  27.  *
  28.  * 0.02a - source cleaned up after being hit on head with Stevens'
  29.  *         book by a dear friend and mentor - Amos Shapira
  30.  *         (amoss@@cs.huji.ac.il). Hooks added for file locks and
  31.  *         complete configuration scan.
  32.  *
  33.  * 0.02b - Complete configuration scan added. Man page written. Locks
  34.  *         still not implemented.
  35.  *
  36.  * 0.02c - Fixed bug in flag test. If the flag file line did not end in
  37.  *         a \n, the test would truncate the last char, thereby making
  38.  *         the test FALSE, even for TRUE values. Locks still not
  39.  *         implemented. Added check for config directory modification at
  40.  *         compile-time.
  41.  *
  42.  * 0.02d - Partially modified into independent functions. Code cleaned.
  43.  *
  44.  * 0.03a - Added "-f" flag to explicitly create a non-existent flag.
  45.  *
  46.  * 0.03b - Added README and changed the Makefike slightly
  47.  *
  48.  * CAVEAT EMPTOR: IN TWO OF ITS THREE MODES, THIS COMMAND RUNS
  49.  * ABSOLUTELY SILENTLY! ABSOLUTELY! THERE IS NO OUTPUT! EVER! IF YOU
  50.  * COMPILE IT TO WRITE FILES IN / AND THEN EXECUTE chkconfig vmlinuz
  51.  * on YOU WILL HAVE A 3-BYTE KERNEL. IF YOU DO THIS, TOUGH! IF YOU
  52.  * BREAK ANYTHING - YOU OWN _BOTH_ HALVES!  
  53.  *
  54.  */
  55.  
  56. #include <stdio.h>
  57. #include <string.h>
  58. #include <unistd.h> 
  59. #include <dirent.h>
  60.  
  61. #define MAJOR_VERSION 0
  62. #define MINOR_VERSION 03
  63. #define REVISION_VERSION b
  64.  
  65. #define MAXLEN 1024
  66.  
  67. #ifndef CONFIG_DIRECTORY
  68. #define CONFIG_DIRECTORY "/etc/config/"
  69. #endif
  70.  
  71. /*
  72.  * For debugging purposes. Linked but not used unless dire
  73.  * circumstances force my hand.
  74.  *
  75.  */
  76.  
  77. extern char *sys_errlist[];
  78. extern int errno;
  79. extern void errexit();
  80.  
  81. int
  82. CheckFlag(char flag[])
  83. {
  84.   char buf [MAXLEN] = CONFIG_DIRECTORY;
  85.   FILE *infp;
  86.  
  87.   strcat (buf, flag);
  88.   
  89.   /* No such flag - silently disprove */
  90.   if ( (infp = fopen (buf, "r") ) == NULL)
  91.     exit ( 1 );
  92.   
  93.   /* failed to read data value - silently disprove */
  94.   if ( (fgets (buf, MAXLEN, infp) ) == NULL)
  95.     exit ( 1 );
  96.   else if (buf [strlen (buf) - 1] == '\n')
  97.     buf [strlen (buf) - 1] = '\0';
  98.   
  99.   return(strcmp (buf, "on") != 0);
  100. }
  101.  
  102. int
  103. SetFlag(char *flag, char *value, char *fmode)
  104. {
  105.   char buf [MAXLEN] = CONFIG_DIRECTORY;
  106.   char type[MAXLEN];
  107.   FILE *infp;
  108.  
  109.   if (!strcmp (fmode, "set") )
  110.     strcpy(type, "r");
  111.   else
  112.     if (!strcmp (fmode, "create") )
  113.       strcpy(type, "w");
  114.     else
  115.       return (1);
  116.  
  117.   if (getuid ())
  118.     return (1);
  119.   else {
  120.  
  121.     strcat(buf, flag);
  122.  
  123.     /* overwrite file or create it */
  124.     if ( (infp = fopen (buf, type) ) == NULL)
  125.       return (1);
  126.     else
  127.       if (!strcmp (type, "r") ) {
  128.     fclose(infp);
  129.     infp = fopen(buf, "w");
  130.       }
  131.  
  132.     /* write flag */
  133.     if ( (fputs (value, infp) ) == EOF)
  134.       return (1);
  135.  
  136.     return (0);
  137.   }
  138. }
  139.  
  140. int
  141. main (ac, av)
  142.      int ac;
  143.      char **av;
  144. {
  145.   FILE *infp;
  146.   DIR  *indp;
  147.   struct dirent *dent;
  148.   char buf [MAXLEN];
  149.   
  150.   switch (ac) {
  151.   case 2 :
  152.     exit (CheckFlag (*++av) );
  153.     break;
  154.   case 4 :
  155.     if ((**av = '-') && (**(av++) = 'f')) {
  156.       /*
  157.        * Cannot use (*++av, *++av) because of precedence. Am unwilling to
  158.        * trust other compilers, either.
  159.        */
  160.       strcpy (buf, *++av);
  161.       exit (SetFlag (buf, *++av, "create") );
  162.     }
  163.     break;
  164.   case 3 :
  165.     /*
  166.      * Cannot use (*++av, *++av) because of precedence. Am unwilling to
  167.      * trust other compilers, either.
  168.      */
  169.  
  170.     strcpy (buf, *++av);
  171.     exit (SetFlag (buf, *++av, "set") );
  172.     break;
  173.   case 1 :
  174.     if ( ( indp = opendir ( CONFIG_DIRECTORY ) ) == NULL )
  175.       exit ( 1 );
  176.     
  177.     fprintf( stdout, "\t%-12s\t\t\t%-5s\n\t%-12s\t\t\t%-5s\n", "Flag",
  178.          "Value", "====", "=====");
  179.     while ( ( dent = readdir ( indp ) ) != NULL )
  180.       if ( ( strcmp ( dent->d_name, "." ) && ( strcmp ( dent->d_name,
  181.                             ".." ) ) ) ) {
  182.     fprintf( stdout, "\t%-12s\t\t\t", dent->d_name );
  183.     strcpy( buf, CONFIG_DIRECTORY );
  184.     strcat( buf, dent->d_name );
  185.     if ( ( infp = fopen ( buf, "r" ) ) != NULL )
  186.       if ( (fgets ( buf, MAXLEN, infp ) ) != NULL ) {
  187.         fprintf ( stdout, "%-s", buf );
  188.         if ( buf [ strlen (buf) - 1 ] != '\n' )
  189.           fprintf ( stdout, "\n" );
  190.       }      
  191.     fclose ( infp );
  192.       }
  193.     /* complete configuration scan */
  194.     exit (1);
  195.   default :
  196.     exit (1);
  197.   }
  198. }
  199.  
  200.